package com.jianbing.shasha.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.jianbing.common.utils.SecurityUtils;
import com.jianbing.shasha.domain.ShashaQuesMusic;
import com.jianbing.shasha.domain.ShashaUserConsolidateQues;
import com.jianbing.shasha.service.IShashaQuesMusicService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jianbing.common.annotation.Log;
import com.jianbing.common.core.controller.BaseController;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.common.enums.BusinessType;
import com.jianbing.shasha.domain.ShashaQuestionBank;
import com.jianbing.shasha.service.IShashaQuestionBankService;
import com.jianbing.common.utils.poi.ExcelUtil;
import com.jianbing.common.core.page.TableDataInfo;

/**
 * 题库Controller
 * 
 * @author ruoyi
 * @date 2024-06-09
 */
@RestController
@RequestMapping("/shasha/bank")
public class ShashaQuestionBankController extends BaseController
{
    @Autowired
    private IShashaQuestionBankService shashaQuestionBankService;
    @Autowired
    private IShashaQuesMusicService shashaQuesMusicService;


    /**
     * 查询题库列表
     */
    // @PreAuthorize("@ss.hasPermi('shasha:bank:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShashaQuestionBank shashaQuestionBank)
    {
        startPage();
        List<ShashaQuestionBank> list = shashaQuestionBankService.selectShashaQuestionBankList(shashaQuestionBank);
        return getDataTable(list);
    }

    /**
     * 导出题库列表
     */
    //  @PreAuthorize("@ss.hasPermi('shasha:bank:export')")
    @Log(title = "题库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShashaQuestionBank shashaQuestionBank)
    {
        List<ShashaQuestionBank> list = shashaQuestionBankService.selectShashaQuestionBankList(shashaQuestionBank);
        ExcelUtil<ShashaQuestionBank> util = new ExcelUtil<ShashaQuestionBank>(ShashaQuestionBank.class);
        util.exportExcel(response, list, "题库数据");
    }

    /**
     * 获取题库详细信息
     */
    //  @PreAuthorize("@ss.hasPermi('shasha:bank:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Map<String,Object> map = new HashMap<>();
        ShashaQuestionBank shashaQuestionBank = shashaQuestionBankService.selectShashaQuestionBankById(id);
        map.put("path",shashaQuestionBank.getPath());
        map.put("levelId",shashaQuestionBank.getLevelId());
        ShashaQuesMusic shashaQuesMusic = new ShashaQuesMusic();
        long num = shashaQuestionBank.getLevelId();
        String type = "";
        if((num-1)%4==0){
            type = "happy";
        } else if ((num-1)%4==1) {
            type = "sad";
        }else if ((num-1)%4==2) {
            type = "angry";
        }else if ((num-1)%4==3) {
            type = "fear";
        }
        shashaQuesMusic.setType(type);
        List<ShashaQuesMusic> shashaQuesMusicList = shashaQuesMusicService.selectShashaQuesMusicList(shashaQuesMusic);
        Collections.shuffle(shashaQuesMusicList);
        map.put("musicPath",shashaQuesMusicList.get(0).getMusicPath());
        return success(map);
    }

    /**
     * 新增题库
     */
    //    @PreAuthorize("@ss.hasPermi('shasha:bank:add')")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShashaQuestionBank shashaQuestionBank)
    {
        return toAjax(shashaQuestionBankService.insertShashaQuestionBank(shashaQuestionBank));
    }

    /**
     * 修改题库
     */
//     @PreAuthorize("@ss.hasPermi('shasha:bank:edit')")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @PutMapping
    public AjaxResult edit(@RequestBody ShashaQuestionBank shashaQuestionBank)
    {
        return toAjax(shashaQuestionBankService.updateShashaQuestionBank(shashaQuestionBank));
    }

    /**
     * 删除题库
     */
    //    @PreAuthorize("@ss.hasPermi('shasha:bank:remove')")
    @Log(title = "题库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shashaQuestionBankService.deleteShashaQuestionBankByIds(ids));
    }


    /**
     * 获取指定数量题目
     */
    //    @PreAuthorize("@ss.hasPermi('shasha:bank:getques')")
    @Log(title = "题库", businessType = BusinessType.DELETE)
    @GetMapping("/testId")
    public AjaxResult getQues(@RequestParam("level") Long levelId,@RequestParam("count") Long count)
    {
        return success(shashaQuestionBankService.getQuesWithCount(levelId,count));
    }



    /**
     * 获取巩固题目
     */
    //   @PreAuthorize("@ss.hasPermi('shasha:bank:getConsolidate')")
    @GetMapping("/consolidate")
    public AjaxResult getConsolidate(@RequestParam("level") Long levelId)
    {
        ShashaUserConsolidateQues shashaUserConsolidateQues = new ShashaUserConsolidateQues();
        shashaUserConsolidateQues.setUserId(SecurityUtils.getUserId());
        shashaUserConsolidateQues.setLevel(levelId);
        return success(shashaQuestionBankService.getConsolidate(shashaUserConsolidateQues));
    }
}
